// Copyright 2007 Google Inc.  All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.connector.afyd;

import com.google.enterprise.connector.spi.Document;
import com.google.enterprise.connector.spi.Property;
import com.google.enterprise.connector.spi.RepositoryException;
import com.google.enterprise.connector.spi.SpiConstants;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Entry;
import com.google.gdata.data.TextConstruct;

import junit.framework.TestCase;

/**
 * This class is a test case that verifies several properties of the
 * EntryDocumentizer implementation, namely:
 * (1) The makeDocument() can extract the required meta data from an entry
 * 
 * @author amsmith@google.com (Adam Smith)
 */
public class EntryDocumentizerTest extends TestCase {

  public void testMakeDocumentExtractsRequiredMetadata() {
    String id = "ID";
    String uri = "http://localhost";
    DateTime dt = DateTime.now();
    String contentNonce = "\u9762" + System.currentTimeMillis();
    
    Entry entry = new Entry();
    entry.setId(id);
    entry.setUpdated(dt);
    entry.addHtmlLink(uri, "en", "example");
    entry.setContent(
        TextConstruct.create(TextConstruct.Type.TEXT, contentNonce, null));
    
    Document doc = null;
    try {
      doc = EntryDocumentizer.makeDocument(entry);
    } catch (RepositoryException re) {
      fail(re.toString());
    }
    
    assertEquals(id,
        getFirstStringValue(doc, SpiConstants.PROPNAME_DOCID));
    assertEquals(uri,
        getFirstStringValue(doc, SpiConstants.PROPNAME_DISPLAYURL));
    assertEquals(dt.toStringRfc822(),
        getFirstStringValue(doc, SpiConstants.PROPNAME_LASTMODIFIED));
    assertTrue(getFirstStringValue(doc, SpiConstants.PROPNAME_CONTENT)
        .contains(contentNonce));
  }
  
  private static String getFirstStringValue(Document doc, String key) {
    try {
      Property prop = doc.findProperty(key);
      return prop.nextValue().toString();
    } catch (RepositoryException re) {
      return null;
    }
  }
  
}
